package com.gitee.sys.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.mybase.top.entity.SysRole;
import com.gitee.sys.mapper.SysRoleMapper;
import com.gitee.sys.service.ISysRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chengkaimao
 * @since 2018-11-17
 */
@Service
@Component
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Override
    public IPage<SysRole> getPage(long current, long size, SysRole sysRole) {
        IPage<SysRole> page = new Page<>(current, size);
        QueryWrapper<SysRole> sysUserQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(sysRole.getRoleCode())){
            sysUserQueryWrapper.like("role_code", sysRole.getRoleCode());
        }
        if(StringUtils.isNotEmpty(sysRole.getRoleName())){
            sysUserQueryWrapper.like("dept_name", sysRole.getRoleName());
        }
        if(StringUtils.isNotEmpty(sysRole.getShopCode())){
            sysUserQueryWrapper.eq("shop_code", sysRole.getShopCode());
        }
        page = this.page(page, sysUserQueryWrapper);
        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveRole(SysRole sysRole) {
        sysRole.setCreateTime(LocalDateTime.now());
        sysRole.setIsLock(0);
        sysRole.setIsValid(1);
        return baseMapper.insert(sysRole);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateRole(SysRole sysRole) {
        UpdateWrapper<SysRole> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("role_code", sysRole.getRoleCode());
        updateWrapper.set("role_name", sysRole.getRoleName());
        updateWrapper.set("shop_code", sysRole.getShopCode());
        updateWrapper.eq("id", sysRole.getId());
        return baseMapper.update(sysRole, updateWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delRole(String roleCode) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_code", roleCode);
        return baseMapper.delete(queryWrapper);
    }
}
